package JDBC.JDBCtest.UI;

import JDBC.JDBCtest.domain.Nember;
import JDBC.JDBCtest.service.Service;

import java.util.Scanner;

/**
 * @author win10
 */
public class MainUI {
    private final Scanner sc = new Scanner(System.in);
    private final Service service;

    public MainUI(Service service) {
        this.service = service;
    }

    //显示
    public static void show() {
        System.out.println("\33[1;31m-\33[0m\33[1;32m-\33[0m\33[1;33m-\33[0m\33[1;31m-\33[0m" +
                "\33[1;35m-\33[0m\33[1;36m-\33[0m\33[1;91m-\33[0m\33[1;92m-\33[0m" +
                "\33[1;93m成\33[0m\33[1;94m员\33[0m\33[1;95m管\33[0m\33[1;96m理\33[0m\33[1;97m系\33[0m\33[1;96m统\33[0m" +
                "\33[1;92m-\33[0m\33[1;91m-\33[0m\33[1;36m-\33[0m\33[1;35m-\33[0m" +
                "\33[1;34m-\33[0m\33[1;33m-\33[0m\33[1;32m-\33[0m\33[1;31m-\33[0m" + "\n"+
                "\33[1;32m[1] 插入" + "\33[0m\n"+
                "\33[1;33m[2] 删除" + "\33[0m\n"+
                "\33[1;34m[3] 修改" + "\33[0m\n"+
                "\33[1;35m[4] 查询" + "\33[0m\n"+
                "\33[1;36m[0] 退出\33[0m");
    }
    //运行
    public void run() throws Exception {
        while(true){
            show();
            int n;
            try{
                n = Integer.parseInt(sc.nextLine());
            }catch (Exception e){
                System.out.println("输入错误！含有非法字符！");
                continue;
            }
            switch (n){
                case 1:insert();break;
                case 2:delete();break;
                case 3:update();break;
                case 4:select();break;
                case 0:
                    System.out.println("感谢您的使用！");
                    System.out.println("程序已关闭...");
                    System.exit(0);
                    close();
                    break;
                default:
                    System.out.println("您输入的编号不正确！");
            }
        }
    }
    //增
    public void insert() {
        try {
            while (true) {
                //子选单
                System.out.println("\33[1;32m[1] 插入数据" + "\n" +
                        "[0] 取消");
                String s = sc.nextLine();
                if (!s.matches("[10]")) {
                    throw new Exception("输入不正确");
                }
                int n = Integer.parseInt(s);

                //判断输入子菜单内容
                if (n == 0) {
                    break;
                } else if (n == 1) {
                    System.out.print("ID:");
                    String id = sc.nextLine();
                    if (!isId(id)) {
                        continue;
                    }
                    //输出业务
                    if (service.select(id, true)) {
                        System.out.println("数据已存在,请重新输入!");
                        continue;
                    }
                    Nember nember = setNember(id);
                    this.service.insert(nember);
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    //删
    public void delete() {
        try {
            while (true) {
                //子选单
                System.out.println("\33[1;33m[1] 删除某位成员" + "\n" +
                        "[2] 删除所有成员" + "\n" +
                        "[0] 取消");
                String s = sc.nextLine();
                if (!s.matches("[120]")) {
                    throw new Exception("输入不正确");
                }
                int n = Integer.parseInt(s);

                //判断输入子菜单内容
                if (n == 0) {
                    break;
                }

                //输出业务
                if (this.service.delete(n)) {
                    System.out.println("数据删除成功!");
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    //改
    public void update() {
        try {
            while (true) {
                //子选单
                System.out.println("\33[1;34m[1] 更新成员" + "\n" +
                        "[0] 取消");
                String s = sc.nextLine();
                if (!s.matches("[10]")) {
                    throw new Exception("输入不正确");
                }
                int n = Integer.parseInt(s);

                //判断输入子菜单内容
                if (n == 0) {
                    break;
                } else if (n == 1) {
                    //根据学号来更新成员
                    System.out.println("请输入要更新的学号信息");
                    String id = sc.nextLine();
                    if (!isId(id)) {  //true已存在/false不存在
                        System.out.println("数据不存在！请重新输入");
                        continue;
                    }
                    if (!this.service.select(id,false)) {
                        continue;
                    }
                    Nember nember = setNember(id);

                    //输出业务
                    if (this.service.update(nember)) {
                        System.out.println("数据更新成功!");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查
    public void select() {
        try{
            System.out.println("\33[1;35m[1] 查询成员" + "\n" +
                    "[2] 看查成员列表" + "\n" +
                    "[0] 取消");
            String s = sc.nextLine();
            if (!s.matches("[120]")) {
                throw new Exception("输入不正确");
            }
            int n = Integer.parseInt(s);

            //判断输入子菜单内容
            if (n == 1) {
                System.out.println("请输入成员ID:");
                String id = sc.nextLine();
                if (isId(id)) {
                    service.select(id, false);
                }
            } else if (n == 2) {
                service.select("*", false);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

//    存储对象
    private Nember setNember(String id) {
        String name = null;
        int age= 0;
        String address= null;
        String phone= null;
        try{
            if(id.length()>11){
                throw new Exception("ID过长！");
            }
            System.out.print("姓名:");
            name=sc.nextLine();
            if(name.length()>11){
                throw new Exception("姓名过长！");
            }
            System.out.print("年龄:");

            age = Integer.parseInt(sc.nextLine());
            if(age<0 || age>500){
                throw new Exception("年龄输入异常");
            }
            System.out.print("住址:");
            address = sc.nextLine();
            if(address.length()>11){
                throw new Exception("住址输入过长！");
            }

            System.out.print("电话:");
            phone = sc.nextLine();
            if(phone.length()>11){
                throw new Exception("电话不能超过11位！");
            }

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return new Nember(id, name, age, address, phone);
    }

    private boolean isId(String id) {
        if(!id.matches("[0-9A-Za-z]+")){
            System.out.println("id非单词");
            return false;
        }
        return true;
    }


    //关闭
    public void close() {
        service.close();
    }
}
